﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using EpubTools;

namespace EpubSqueaker
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public delegate void UpdateDirectoryDelegate();
		private IEnumerator<string> CurrentDir = null;

		public MainWindow()
		{
			InitializeComponent();
			Progress.Visibility = System.Windows.Visibility.Hidden;
			RunModeDir.IsChecked = true;
		}

		private void btnGo_Click(object sender, RoutedEventArgs e)
		{
			windowInitialize();

			if ((bool)RunModeFile.IsChecked)
			{
				var dialog = new Microsoft.Win32.OpenFileDialog();
				var dialogResult = dialog.ShowDialog();
				if (dialogResult == true)
				{
					updateFile(dialog.FileName);
					windowFinalize(true);
				}
				else
				{
					windowFinalize(false);
				}
			}
			else if ((bool)RunModeDir.IsChecked)
			{
				var dialog = new System.Windows.Forms.FolderBrowserDialog();
				var dialogResult = dialog.ShowDialog();
				if (dialogResult == System.Windows.Forms.DialogResult.OK)
				{
					string bookPath = dialog.SelectedPath;

					var a = Directory.GetFiles(bookPath, "*.epub", System.IO.SearchOption.AllDirectories).GetEnumerator();

					var useSubdirs = (bool)RunModeSubDirs.IsChecked ? System.IO.SearchOption.AllDirectories : System.IO.SearchOption.TopDirectoryOnly;
					CurrentDir = Directory.EnumerateFiles(bookPath, "*.epub", useSubdirs).GetEnumerator();

					btnGo.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateDirectoryDelegate(updateNextFile));
				}
				else
				{
					windowFinalize(false);
				}
			}
			else
			{
				// Nothing is checked. This is a problem.
				throw new ApplicationException("No button is selected - how?");
			}
		}

		private void updateNextFile()
		{
			if (CurrentDir != null)
			{
				updateFile(CurrentDir.Current);

				if (CurrentDir.MoveNext())
				{
					btnGo.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.SystemIdle,
						new UpdateDirectoryDelegate(this.updateNextFile));
				}
				else
				{
					CurrentDir = null;
					windowFinalize(true);
				}
			}
		}

		#region Progress area update methods
		private void windowInitialize()
		{
			btnGo.IsEnabled = false;
			btnGo.Content = "Running";

			Progress.Visibility = System.Windows.Visibility.Visible;
			ProgressFile.Text = "";
			UpdateFile.Text = "";
		}

		private void progressUpdateStart(string filename)
		{
			ProgressFile.Text = "Processing file: " + filename;
		}

		private void progressUpdateFinish(string filename, bool isUpdated)
		{
			ProgressFile.Text = "Processed file: " + filename;
			if (isUpdated)
			{
				UpdateFile.Text = "Last updated file: " + filename;
			}
		}

		private void windowFinalize(bool success)
		{
			if (success)
			{
				ProgressFile.Text = "Processing complete.";
			}
			else
			{
				ProgressFile.Text = "Processing canceled.";
			}

			btnGo.Content = "Go";
			btnGo.IsEnabled = true;
		}
		#endregion

		private void updateFile(string file)
		{
			progressUpdateStart(file);
			bool isUpdated = false;
			NookTools.updateCoverMetadata(file, out isUpdated);
			progressUpdateFinish(file, isUpdated);
		}
	}
}
